var express = require('express');
var router = express.Router();
let jwt = require("jsonwebtoken");
let leaveslistService = require("../service/leaveslistService");

/* GET home page. */
router.get('/', function (req, res, next) {
    res.render('index', { title: 'Express' });
});
router.post('/addleave', async (req, res, next) => {
    let { reson, start, end, extend, type = 1 } = req.body;//获取传入的数据

    //获取并解析传入的请求头
    let token = req.headers.token;
    jwt.verify(token, '2215', async (err, info) => {
        if (err) {
            res.json({
                code: 201,
                msg: 'token解析异常'
            })
        }
        else {
            if (info.usertype === 2) {  //筛选用户权限
                let result = await leaveslistService.addleave({
                    userid: info.userid,
                    username: info.username,
                    reson,
                    start,
                    end,
                    extend,
                    type
                })
                res.json({
                    code: 200,
                    list: result,
                    msg: '申请成功',
                })
            } else {
                res.json({
                    code: 202,
                    msg: '权限异常'
                })
            }
        }
    })
})
// router.get('/getleaveall', (req, res, next) => {
//     let token = req.headers.token;
// })


router.get('/getleave', (req, res, next) => {
    let token = req.headers.token;
    jwt.verify(token, '2215', async (err, info) => {
        if (err) {
            res.json({
                code: 201,
                msg: 'token解析异常'
            })
        } else {
            if (info.usertype == 2) {
                let userid = info.userid;
                let result = await leaveslistService.getleave({
                    userid
                });
                res.json({
                    code: 200,
                    msg: '学生',
                    list: result
                })
            }
            else if (info.usertype == 1) {
                let result = await leaveslistService.getleave({});
                res.json({
                    code: 200,
                    msg: '老师',
                    list: result
                })
            } else {
                res.json({
                    code: 202,
                    msg: '权限异常',
                    info
                })
            }
        }
    })
})


router.patch("/updatatype", (req, res, next) => {
    let token = req.headers.token;
    let { leaveid, type } = req.body
    jwt.verify(token, '2215', async (err, info) => {
        if (err || info.usertype != 1) {
            res.json({
                code: 201,
                msg: 'token解析异常'
            })
        } else {
            let result = await leaveslistService.updatatype({ _id: leaveid }, { $set: { type } });
            res.json({
                code: 200,
                msg: '',
                result
            })
        }
    })

})
module.exports = router;
